import CreateDatabase from "../components/databaseInfo.vue"
import tableInfo from "../components/tableInfo.vue"
import {
	SqlBuilder
} from './SqlBuilder'
import {
	metadataCovert,
	metaDefaultConfg
} from "./constant"
import {ScriptApi} from "../api/scriptApi"
class ResouceTable {
	constructor(opt, params) {
		this.opt = opt;
		this.params = params
		this.node = params.node;
		this.messageBox = opt.messageBox;
		this.sqlBuilder = new SqlBuilder();
		this.scriptApi = new ScriptApi(opt.request,opt.message)
	}

	getDataViewConfig() {
		return {
			buildScript: () => {
				const {
					connId,
					catalog,
					tableName
				} = this.node.data.properties
				return this.sqlBuilder.selectTableData(catalog,tableName)
			},
			display: (item) => {
				const {
					connId,
					catalog,
					tableName,
					puginId
				} = this.node.data.properties
				const executeResultResizerValue = "100%"
				const params = {
					connId,
					catalog,
					tableName,
					puginId,
					item,
					executeResultResizerValue
				}
				return {
					id: `${this.node.id}`,
					type: this.opt.config.CONTEXT_MENU_TYPE_TAGS_COM,
					title: `${catalog}@${tableName}-table`,
					data: params,
					component: 'ExecuteResult'
				}
			}
		}
	}


	getContextMenus() {
		let menus = [];

		menus.push({
			label: "设计表",
				dividedTop: true,
			click: (event, item) => {
				const {
					connId,
					catalog,
					tableName,
					puginId
				} = this.node.data.properties
				const params = {
					connId,
					catalog,
					puginId,
					tableName
				}
				this.opt.bus.$emit(this.opt.config.CONTEXT_MENU_EVENT_KEY, {
					id: `${this.node.id}_design`,
					type: this.opt.config.CONTEXT_MENU_TYPE_TAGS_COM,
					title: `${catalog}@${tableName}-表`,
					data: params,
					component: tableInfo
				})
				return true;
			},
		})

		menus.push({
			label: "删除数据库",
			dividedButtom: true,
			click: () => {
				const catalogName = this.node.data.properties.SCHEMA_NAME;
				this.messageBox.confirm(
						`确定要删除[${catalogName}]?`,
						'消息', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning',
						}
					)
					.then(async () => {
						let sql = this.sqlBuilder.deleteDatabaseCovert(catalogName);
						const executeParams = {
							script: sql
						};
						const params = {
							connId: this.node.data.properties.connId,
							executeParams: executeParams
						};
						let res = await this.opt.request.post(`/sqlDatabase/executeScript`, params);
						//没有数据就设置空数据
						if (!res.data) {
							return;
						}
						const result = res.data[0];
						if (result.success) {
							this.opt.message.success('删除成功');
							//刷新列表
							this.opt.bus.$emit(this.opt.config.REFRESH_NODE, this.node.parent)
							return true;
						} else {
							this.opt.message.error(result.message);
							return false;
						}
					})
					.catch((e) => {
						return true;
					})

			},
		})
		menus.push({
			label: "新建查询",
			dividedButtom: true,
			click: () => {
				this.opt.bus.$emit(this.opt.config.CONTEXT_MENU_EVENT_KEY, {
					type: this.opt.config.CONTEXT_MENU_TYPE_TAGS_COM,
					title: '新建查询',
					id: this.node.data.id + "_sql_query",
					component: 'WorkbeanchSqlEditor',
					data: {
						connId: this.node.data.properties.connId,
						catalog: this.node.data.properties.catalog
					}
				})
				return true;
			},
		})
		menus.push({
			label: "运行sql文件",
			click: () => {
				return false;
			},
		})
		menus.push({
			label: "导出结构",
			click: () => {
				return false;
			},
		})
		menus.push({
			label: "导出结构和数据",
			click: () => {
				return false;
			},
		})

		return menus
	}
}

export {
	ResouceTable
}
